<?php
	session_start();
	chdir("../../");
	require ("PHP/config/main.conf.php");

	
	$userId = $_SESSION['usrId']; //for the time being hardcoded
	$newCampaign = addslashes($_POST['newCampaign']);
	$newsName = addslashes($_POST['newsName']);
	$themeId  = $_POST['th_sel'];
	$campaignId = $_POST['ca_sel'];
	 $noLanguage=0;
	
	
	$usrId = $_SESSION['usrId'];

	

	
	if (!$campaignId) $campaignId = -1;

	if (!$newsName)
	{
		header("Location: /newNews/creat_campaign.php?usrId=$userId");
		
		
		
		exit;
	}
	
	//empty theme
	if (!$themeId || $themeId == "-1")
	{
			header("Location: /newNews/creat_campaign.php?usrId=$userId");
		
		
	
		
		exit;
	}
	
	
	if ($campaignId==-1 && $newCampaign!='')
	{
		//echo "Create new campaign";
		$campaignId = Model::pushCampaign($newCampaign, $userId);
		if (!$campaignId)
		{
			header("Location: /newNews/creat_campaign.php?usrId=$userId");
			exit;
		}
	}
	elseif ($campaignId!=-1)
	{
		//echo "using existing campaign";
	}
	else
	{
		header("Location: /newNews/creat_campaign.php?usrId=$userId");
		exit;
	}

	
	//empty newsletter name
	

	$newsId = Model::pushNews($newsName, $campaignId, $themeId);
	// $hotelList = Model::getHotelAdminForUser($usrId);
	 $hotelList = Model::getHotelAdminForUser("1");
	if($hotelList)
	{
		foreach($hotelList as $hotelDetail)
		{
			$hid = $hotelDetail['hotelId'];
			if ($_POST[$hid])
			Model::pushNewsAttributes($newsId,"hotel",$hid);
		}
		
	
	}
	
		$languageOption = Model::getLanguageForAll();
		$flagLg = 0;
		foreach($languageOption as $languageOptionDetail)
		{
			
			
			if ($_POST[$languageOptionDetail['nameEntire']])
			{
				
				Model::pushNewsAttributes($newsId,"lg",$languageOptionDetail['name']);
				$language = $languageOptionDetail['name'];
				$flagLg = 1;
			}
		}
		if($flagLg == 0 )
		{
			$language = 'en';
			Model::pushNewsAttributes($newsId,"lg","en");
		}
		
		
	
	

	header("Location: /newNews/news.php?campaignId=$campaignId&newsId=$newsId&language=$language");
	exit;

?>
